package com.yangye.otherdemo.contended;

import sun.misc.Contended;

/**
 * 使用@Contended注解缓存行对齐
 */
public class ContendedTest {

	public static long count = 100000000L;

	@Contended
	volatile long x;
	@Contended
	volatile long y;

//	volatile long x;
//	volatile long y;

	public static void main(String[] args) throws InterruptedException {
		ContendedTest t = new ContendedTest();

		long start = System.currentTimeMillis();
		Thread t1 = new Thread(() -> {
			for (int i = 0; i < count; i++) {
				t.x = i;
			}
		});
		Thread t2 = new Thread(() -> {
			for (int i = 0; i < count; i++) {
				t.y = i;
			}
		});
		t1.start();
		t2.start();
		t1.join();
		t2.join();
		long end = System.currentTimeMillis();
		System.out.println(end - start);
	}
}
